﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Threading;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;

namespace VisualRibbonEditor
{
	public class ServiceWrapper
	{
		private IOrganizationService _svc;
		public ServiceWrapper(IOrganizationService svc)
		{
			_svc = svc;
		}
		public OrganizationResponse Execute(OrganizationRequest request)
		{
			return _svc.Execute(request);
		}
		public void ExecuteAsync( OrganizationRequest request , Action<OrganizationResponse> callback )
		{
			OrganizationResponse resp = _svc.Execute(request);
			callback(resp);
		}
		 
		public void CreateAsync( Entity entity, Action<Guid> callback )
		{
			Guid id = _svc.Create(entity);
			callback(id);
		}

		public Entity Retrieve(string entityName, Guid id, ColumnSet cols)
		{
			return _svc.Retrieve(entityName, id, cols);
		}
		public void RetrieveAsync( string entityName, Guid id, ColumnSet cols, Action<Entity> callback )
		{
			Entity ent = _svc.Retrieve(entityName, id, cols);
			callback(ent);
		}

		public EntityCollection RetrieveMultiple(QueryBase query)
		{
			return _svc.RetrieveMultiple(query);
		}


		public void RetrieveMultipleAsync( QueryBase query, Action<EntityCollection> callback )
		{
			EntityCollection ec = _svc.RetrieveMultiple(query);
			callback(ec);
		}

		public void UpdateAsync( Entity entity, Action callback)
		{
			_svc.Update(entity);
			callback();
		}

		public void DeleteAsync( string entityName, Guid id, Action callback)
		{
			_svc.Delete(entityName, id);
			callback();
		}

	}
}
